<template>
	 
		<!--编辑界面 XmWorkload 工时登记表 bizType 报工类型1-任务，2-缺陷，3-测试用例设计，4-测试执行-->
			<el-form :model="editForm" label-width="120px"  :rules="editFormRules" ref="editFormRef" label-position="right">
 
				<el-row v-if="bizType=='1' && editForm.ntype=='1'" :gutter="10">
					<el-col :span="14">
						<el-form-item label="预估工时:" prop="initWorkload" title="指一个人(按2-4年工作经验标准算)完成当前任务需要的工作时长。">
						 
							<el-space>

							<el-input size="small" :readonly="editForm.calcType!='1'"  :controls="false" type="number" :step="8"    v-model="editForm.initWorkload" placeholder="计划预估工时" @change="editSomeFields(editForm,'initWorkload',$event)">
								<template #suffix>
									H	<mdp-select style="margin-bottom:0px;margin-right:-6px;"  title="手工填写：预估工时将不允许自动更新；自动汇总：预估工时自动从下级预估工时进行同步" itemCode="xm_calc_type" placeholder="统计方式" v-model="editForm.calcType" :clearable="false"  @change="editSomeFields(editForm,'calcType',$event)"/> 

								</template>	
							</el-input>
							<el-text  type="info" title="来自下一级计划或任务的预估工时汇总，当下级变动后自动同步">预估参考:{{$mdp.toFixed(editForm.sonSumInitWorkload||0)}}H</el-text>
							 
						</el-space>
						</el-form-item>
					</el-col>   
					<el-col :span="10">
						<el-form-item label="已完成工时:" prop="sumActWorkload">  
							{{ $mdp.toFixed(editForm.sumActWorkload) }}H &nbsp;<el-tag type="warning" title="进度"> {{ latestVal.initRate}}%</el-tag>
							&nbsp;<icon icon="ep:question-filled" title="来自下一级计划或任务的已完成工时汇总，当下级变动后自动同步"/> 
 						</el-form-item>
					</el-col> 
				</el-row>
				<el-row v-if="bizType=='1' && editForm.ntype=='0'" :gutter="20">   
						<el-col :span="8">
							<el-form-item label="预估工时:" prop="initWorkload" title="指一个人(按2-4年工作经验标准算)完成当前任务需要的工作时长。"> 
								<el-input size="small"  :controls="false" type="number" :step="8"    v-model="editForm.initWorkload" placeholder="计划预估工时" @change="editSomeFields(editForm,'initWorkload',$event)">
								<template #suffix>
									H
								</template>	
							</el-input>
							</el-form-item>
						</el-col>   
						<el-col :span="10">
							<el-form-item label="已登记工时:" prop="sumActWorkload">  
								{{ $mdp.toFixed(editForm.sumActWorkload) }}H &nbsp;<el-tag type="warning" title="进度"> {{ latestVal.initRate}}%</el-tag>
								&nbsp;<icon icon="ep:question-filled" title="来自任务报工工时汇总，任务报工后自动同步"/> 
							</el-form-item>
						</el-col> 
				</el-row> 
				<el-row v-if="bizType=='5' && editForm.dclass<'3'" :gutter="10">
					<el-col :span="14">
						<el-form-item label="预估工时:" prop="initWorkload" title="指一个人(按2-4年工作经验标准算)完成当前需求需要的工作时长。">
						 
							<el-space>

							<el-input size="small" :readonly="editForm.calcType!='1'"  :controls="false" type="number" :step="8"    v-model="editForm.initWorkload" placeholder="计划预估工时" @change="editSomeFields(editForm,'initWorkload',$event)">
								<template #suffix>
									H	<mdp-select style="margin-bottom:0px;margin-right:-6px;"  title="手工填写：预估工时将不允许自动更新；自动汇总：预估工时自动从下级预估工时进行同步" itemCode="xm_calc_type" placeholder="统计方式" v-model="editForm.calcType" :clearable="false"  @change="editSomeFields(editForm,'calcType',$event)"/> 

								</template>	
							</el-input>
							<el-text  type="info" title="来自下一级特性或者故事的预估工时汇总，当下级变动后自动同步">预估参考:{{$mdp.toFixed(editForm.sonSumInitWorkload||0)}}H</el-text>
							 
						</el-space>
						</el-form-item>
					</el-col>   
					<el-col :span="10">
						<el-form-item label="已完成工时:" prop="sumActWorkload">  
							{{ $mdp.toFixed(editForm.sumActWorkload) }}H &nbsp;<el-tag type="warning" title="进度"> {{ latestVal.initRate}}%</el-tag>
							&nbsp;<icon icon="ep:question-filled" title="来自下一级特性或者故事的已完成工时汇总，当下级变动后自动同步"/> 
 						</el-form-item>
					</el-col> 
				</el-row>
				 
				<el-row v-if="bizType=='5' && editForm.dclass=='3'" :gutter="10">
					<el-col :span="14">
						<el-form-item label="预估工时:" prop="initWorkload" title="指一个人(按2-4年工作经验标准算)完成当前需求需要的工作时长。">
						 
							<el-space>

							<el-input size="small" :readonly="editForm.calcType!='1'"  :controls="false" type="number" :step="8"    v-model="editForm.initWorkload" placeholder="计划预估工时" @change="editSomeFields(editForm,'initWorkload',$event)">
								<template #suffix>
									H	<mdp-select style="margin-bottom:0px;margin-right:-6px;" title="手工填写：预估工时将不允许自动更新；自动汇总：预估工时自动从任务预估工时进行同步" itemCode="xm_calc_type" placeholder="统计方式" v-model="editForm.calcType" :clearable="false"  @change="editSomeFields(editForm,'calcType',$event)"/> 
								</template>	
							</el-input>
							<el-text  type="info" title="来自任务的预估工时汇总，当执行需求进度统计时，自动同步">预估参考:{{$mdp.toFixed(editForm.sonSumInitWorkload||0)}}H</el-text>
							 
						</el-space>
						</el-form-item>
					</el-col>   
					<el-col :span="10">
						<el-form-item label="已完成工时:" prop="sumActWorkload">  
							{{ $mdp.toFixed(editForm.sumActWorkload) }}H &nbsp;<el-tag type="warning" title="进度"> {{ latestVal.initRate}}%</el-tag>
							&nbsp;<icon icon="ep:question-filled" title="来自任务的已完成工时汇总，当执行需求进度统计时，自动同步"/> 
 						</el-form-item>
					</el-col> 
				</el-row>
			</el-form> 
			<XmWorkloadMng v-if="parentOpType!='add' && ((bizType=='1'&& editForm.ntype=='0')||(bizType=='5' && editForm.dclass=='3')||bizType=='2'||bizType=='3'||bizType=='4')" :visible="visible" :biz-type="bizType" :xm-task="xmTask" :xm-menu="xmMenu"   @add-submit="onWorkloadSubmit" />
 	
</template>

<script>
	import util from '@/components/mdp-ui/js/util';//全局公共库
	import config from "@/api/mdp_pub/mdp_config"; //全局公共库import

	import { getTaskDetail, editXmTaskSomeFields } from '@/api/xm/core/xmTask'; 
	import { editXmMenuStateSomeFields,queryXmMenuStateById } from '@/api/xm/core/xmMenuState'; 
	import { editXmMenuSomeFields,queryXmMenuById } from '@/api/xm/core/xmMenu'; 
	import { mapState } from 'pinia'
import { useUserStore } from '@/store/modules/user'
	import XmWorkloadMng from './XmWorkloadMng.vue';

	export default {
	    name:'xmWorkloadEdit',
	    components: {
				XmWorkloadMng,
        },
		computed: {
		    ...mapState(useUserStore,[ 'userInfo'  ]),
			initVal(){

				var params={}
				if( this.bizType=='1'){
					params.id=this.xmTask.id
					params.initWorkload=this.xmTask.initWorkload
					params.budgetWorkload=this.xmTask.budgetWorkload
					params.actWorkload=this.xmTask.actWorkload
					params.ntype=this.xmTask.ntype
					params.rate=this.xmTask.rate
					params.initRate=this.xmTask.initRate||0
					params.calcType=this.xmTask.calcType||(params.ntype=='0'?'1':'2')
					params.mactWorkload=this.xmTask.mactWorkload
					params.sumActWorkload=this.xmTask.sumActWorkload
					params.sonSumInitWorkload=this.xmTask.sonSumInitWorkload
					params.sonSumMactWorkload=this.xmTask.sonSumMactWorkload
				}
				if(  this.bizType=='5' ){
					params.menuId=this.xmMenu.menuId
					params.initWorkload=this.xmMenu.initWorkload
					params.budgetWorkload=this.xmMenu.budgetWorkload
					params.actWorkload=this.xmMenu.actWorkload
					params.ntype=this.xmMenu.ntype 
					params.finishRate=this.xmMenu.finishRate
					params.initRate=this.xmMenu.initRate||0
					params.dclass=this.xmMenu.dclass 
					params.calcType=this.xmMenu.calcType||'2'
					params.mactWorkload=this.xmMenu.mactWorkload
					params.sumActWorkload=this.xmMenu.sumActWorkload
					params.sonSumInitWorkload=this.xmMenu.sonSumInitWorkload
					params.sonSumMactWorkload=this.xmMenu.sonSumMactWorkload
				}
				 
				return params
			},
			latestVal(){ 
				let p={sumActWorkload:this.editForm.sumActWorkload,initWorkload:this.editForm.initWorkload||0,initRate:this.editForm.initRate||0}
				if(this.editForm.calcType=='2'){
					p.initWorkload=this.editForm.sonSumInitWorkload||0 + this.editForm.mactWorkload||0
				} 
				if(p.initWorkload>0){
					p.initRate=Math.round(parseFloat(p.sumActWorkload||0) *100 / parseFloat(p.initWorkload))
				}  
				return p;
			}

		},
		props:['xmTask','visible','bizType'/*报工类型1-任务，2-缺陷，3-测试用例设计，4-测试执行 */,'parentOpType'/** 父组件操作add,edit*/,
		'xmMenu'],

		watch: { 
			initVal:{
				handler(){
					this.initData();
				},
				deep:true,
			}
	    },
		data() {
			return {
 				load:{ list: false, edit: false, del: false, add: false },//查询中...
				dicts:{},//下拉选择框的所有静态数据 params={categoryId:'all',itemCodes:['sex']} 返回结果 {sex: [{id:'1',name:'男'},{id:'2',name:'女'}]}
				editFormRules: {
					id: [
						//{ required: true, message: '主键不能为空', trigger: 'blur' }
					]
				},//新增界面数据 xm_task
				editForm: {
					initRate:0,
					id:'',name:'',parentTaskid:'',parentTaskname:'',projectId:'',projectName:'',level:'3',sortLevel:'0',executorUserid:'',executorUsername:'',
					preTaskid:'',preTaskname:'',startTime:'',endTime:'',milestone:'',description:'',remarks:'',createUserid:'',createUsername:'',createTime:'',taskOut:'0',
					rate:0,budgetCost:'',budgetWorkload:'',actCost:'',actWorkload:'',taskState:'0',taskClass:'0',toTaskCenter:'0',actStartTime:'',actEndTime:'',taskType:'4',planType:'w2',settleSchemel:'1',ntype:'0',childrenCnt:0
					,rworkload:null,dclass:'',
				},
        maxTableHeight:300,
			}//end return
		},//end data
		methods: {
			// 取消按钮点击 父组件监听@cancel="editFormVisible=false" 监听
			handleCancel:function(){
				this.$emit('cancel');
			},
			//新增、编辑提交XmWorkload 工时登记表父组件监听@submit="afterEditSubmit"
			saveSubmit: function () {
			},
			initData: function(){

				this.editForm=Object.assign({},this.initVal)
				this.editFormBak=Object.assign({},this.editForm)
            },



			editSomeFields(row,fieldName,$event){ 
					if(!$event){
						this.$message.error("请输入正确的值")
						row[fieldName]=this.editFormBak[fieldName]
						return ;
					}  
				var emit="edit-fields"
				var params={$pks:[row.id]};
				if(this.parentOpType=='add'){
					params[fieldName]=$event
					this.$emit(emit,params);
					return;
				}
				var func=null;
				
				if(this.bizType==='1'){
					func=editXmTaskSomeFields

				}else if(this.bizType==='5'){
					params['$pks']=[row.menuId]
					 func=editXmMenuSomeFields 
				}
				params[fieldName]=$event
				func(params).then(res=>{
					var tips = res.tips;
					if(tips.isOk){
						Object.assign(params,this.latestVal)
						this.$emit(emit,params);
						this.$message.success("更新成功")
					}else{
						this.editForm=Object.assign(this.editForm,this.editFormBak)
						this.$notify({position:'bottom-left',showClose:true,message:tips.msg,type:tips.isOk?'success':'error'})
					}
				})
			},
			onWorkloadSubmit(){
				
				if(this.bizType=='1'){
					getTaskDetail({id:this.xmTask.id}).then(res=>{
						var tips = res.tips 
						if(tips.isOk){
							if(res.data){
								//Object.assign(this.xmTask,res.data[0])
								let xmTask=res.data
								Object.assign(this.editForm,xmTask)
								Object.assign(this.editFormBak,xmTask)
								this.$emit('edit-fields',this.editForm)
							}
						}
					})
				}
			}
		},//end method
		mounted() {
		    this.$nextTick(() => {

                this.initData()
            });
		}
	}

</script>

<style scoped>

</style>
